home *** CD-ROM | disk | FTP | other *** search
- Path: news1.h1.usa.pipeline.com!usenet
- From: grantp@usa.pipeline.com(Pete)
- Newsgroups: comp.lang.c++
- Subject: Re: Memory problems in BC++3.1 for Win.
- Date: 24 Feb 1996 10:19:42 GMT
- Organization: Kalevi, Inc.
- Message-ID: <4gmonu$cgl@news1.usa.pipeline.com>
- NNTP-Posting-Host: pipe4.h1.usa.pipeline.com
- X-PipeUser: grantp
- X-PipeHub: usa.pipeline.com
- X-PipeGCOS: (Pete)
- X-Newsreader: Pipeline USA v3.3.0
-
- On Feb 23, 1996 18:33:16 in article <Memory problems in BC++3.1 for Win.>,
- '"Michael J. Cuccia" <z_cucciamj@titan.sfasu.edu>' wrote:
-
-
- >When I try to run a program under Borland's Turbo C++ for
- >Windows 3.1, I get a stack fault when I try to allocate an
- >array of only 100 structures of about 20 characters in
- >length. I went to the compile|code generation option and
- >made the default memory model LARGE, but that had no
- >effect. Any suggestions?
- >
- >P.S. I have 8 MB RAM.
- >
- You are using automatic allocation; i.e., on the stack.
- Switch to using dynamic instead.
-
- On machines with rather limited stack space, e.g., the PC,
- the general rule to follow is to use the stack only for trivial
- ephemeral memory requirements an use dynamic for arrays
- and large objects. Like any rule of thumb, however, use it
- with some discretion -- there's no substitute for intelligent
- thought.
-
- Anyway, the bottom line (now that I'm off the soap box:-))
-
- Instead of:
- MyStruct x[100];
-
- use:
- MyStruct * z = new MyStruct[100];
-
- Of course, now that you've allocated the objex from the
- global heap, you must also free the memory when finished
- with it:
-
- delete [] z;
-
- Of course, you could increase your stack size, but that's not
- a desirable option for "real" programs. Learn to use dynamic
- allocation at an early stage and you're a small jump ahead of
- the others in your class -- er, I mean your school class.
-
- (Looks like I'm in one of my soap-boxy moods today!)
-
- --
- Pete Grant
- Kalevi, Inc.
- Software Engineering & development
-